<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/*****
  * ErkanaAuth is an easy to use, non-invasive, use authentication library
  * @author     Michael Wales
  * @email      webmaster@michaelwales.com
  * @filename   auth.php
  * @title      ErkanaAuth
  * @url        http://www.michaelwales.com/
  * @version    2.0
  *****/
 
class Auth {
 
    var $CI;
	
    function __construct() {
        $this->CI =& get_instance();
        log_message('debug', 'Authorization class initialized.');
		
        $this->CI->load->database();
    }
	
    /***
	Determines whether the passed condition is valid to login a user, if so - sets a session variable containing the user's ID
        * @param    $condition array    The condition to query the database for
        * @return   boolean
	***/
    function try_login($condition = array()) {
        $this->CI->db->select('id');
        $query = $this->CI->db->getwhere('users', $condition, 1, 0);
        if ($query->num_rows != 1) {
            return FALSE;
        } else {
            $row = $query->row();
            $this->CI->session->set_userdata(array('user_id'=>$row->id));
            return TRUE;
        }
    }
    
    /***
	Logs out a user by deleting their session variables
        * @return   null
	***/
    function logout() {
        $this->CI->session->set_userdata(array('user_id'=>FALSE));
    }
    
    /***
    Returns an object containing user information via the user's ID
        * @param    $id integer         The user's ID
        * @return   object              Upon valid user
        * @return   FALSE               Upon invalid user         
	***/
    function get_user($id) {
        if ($id) {
            $query = $this->CI->db->getwhere('users', array('users.id'=>$id), 1, 0);
            if ($query->num_rows() == 1) {
                return $query->row();
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }
}